<template>
    <div class="login-container">
        <el-card class="login-card">
            <div class="title">预制菜溯源系统</div>
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="80px">
                <el-form-item label="账号" prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名或手机号"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="text" @click="goToRegister" style="width: 100%">没有账号？立即注册</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: 'Login',
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名或手机号', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleLogin() {
                this.$refs.loginForm.validate(async (valid) => {
                    if (valid) {
                        try {
                            const response = await this.$axios.post('/api/login', this.loginForm)
                            
                            // 保存token和用户信息
                            localStorage.setItem('token', response.data.token)
                            localStorage.setItem('user', JSON.stringify(response.data.user))
                            
                            this.$message.success('登录成功')
                            this.$router.push('/')
                        } catch (error) {
                            console.error('登录错误:', error)
                            this.$message.error((error.response && error.response.data && error.response.data.message) || '登录失败，请检查网络连接')
                        }
                    }
                })
            },
            goToRegister() {
                this.$router.push('/register')
            }
        }
    }
</script>

<style scoped>
    .login-container {
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #f3f3f3;
    }
    
    .login-card {
        width: 400px;
    }
    
    .title {
        text-align: center;
        font-size: 24px;
        margin-bottom: 30px;
        color: #333;
    }
</style>